package com.example.wendao.view.ui.fragment;


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.User;
import com.example.wendao.view.adapter.AgreementForumListAdapter;
import com.example.wendao.viewmodel.ForumViewModel;
import com.example.wendao.viewmodel.UserViewModel;


/**
 * A simple {@link Fragment} subclass.
 */
public class ViewForumFragment extends Fragment {
    //private ActivityPagingTestBinding binding;
    private AgreementForumListAdapter adapter = new AgreementForumListAdapter();
    private ForumViewModel forumViewModel = null;
    private UserViewModel userViewModel = null;
    private String[] vforumId;
    private int[] arr;
    private String SQL = "";
    private User user;
    private RecyclerView recyclerView;

    public ViewForumFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_viewforum, container, false);
        recyclerView.findViewById(R.id.rv_viewforum);
        forumViewModel = new ViewModelProvider(this).get(ForumViewModel.class);
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);
        //将user中的点赞id字段提取出来到arr数组中
        user = userViewModel.getCurrentUser();
        vforumId=user.getBforumId().split(",");
        for (int i = 0; i < vforumId.length; i++) {
            arr[i] = Integer.parseInt(vforumId[i]);
            //  forumViewModel.setId(arr[i]);
            // forumViewModel.getForumById().observe(this.getViewLifecycleOwner(), adapter::submitList);
        }
        //将arr数组中的id封装成一段sql语句
        if(arr.length > 0) {
            SQL += " '";
            for(int i=0; i<arr.length-1; i++) {
                SQL = SQL+ arr[i] + "','";
            }
            SQL += arr[arr.length-1] + "'";
        }
        forumViewModel.getForumsByForumIds(SQL).observe(this.getViewLifecycleOwner(), adapter::submitList);
        PageUtils.initRecyclerView(recyclerView, adapter, new LinearLayoutManager(this.getActivity()));
        return view;
    }

}
